<template>
  <div>
    <loader v-if="musicList.length === 0"></loader>
    <table v-else>
      <thead>
        <tr>
          <th></th>
          <th>标题</th>
          <th>时长</th>
          <th>歌手</th>
        </tr>
      </thead>
      <tbody>
        <tr
          :class="{behind: true,current: index%2 !== 0}"
          v-for="(item,index) in behindList"
          :key="item.id"
        >
          <td>{{index+1}}</td>
          <td @click="toSongDetail(item.id)">
            <div class="name">
              <img :src="item.al.picUrl" :alt="item.name" />
              <span class="play"></span>
              {{item.name}}
            </div>
          </td>
          <td>{{item.dt | timer}}</td>
          <td>
            <div class="artist">{{item.ar[0].name}}</div>
          </td>
        </tr>
        <tr
          :class="{after: true,current: index%2 === 0}"
          v-for="(item,index) in afterList"
          :key="item.id"
        >
          <td>{{index+4}}</td>
          <td @click="toSongDetail(item.id)">
            <div class="name">
              <span class="play"></span>
              {{item.name}}
            </div>
          </td>
          <td>{{item.dt | timer}}</td>
          <td>
            <div class="artist">{{item.ar[0].name}}</div>
          </td>
        </tr>
      </tbody>
    </table>
  </div>
</template>

<script>
import { initData } from './initMixin'

import { formatDate } from '@/common/format'
import { _musicList } from '@/network/discover'

import Loader from '@/components/common/loader/Loader'

export default {
  mixins: [initData],
  components: {
    Loader
  },
  data() {
    return {
      musicList: [],
      behindList: [],
      afterList: []
    }
  },
  filters: {
    timer(time) {
      return formatDate(time, 'mm:ss')
    }
  },
  mounted() {
    this.$bus.$on('itemClick', (item) => {
      this.id = item.id
      _musicList(item.id).then(res => {
        this.musicList = res.playlist.tracks
        // 将前三和后面的分开
        this.behindList = this.musicList.slice(0, 3)
        this.afterList = this.musicList.slice(3)
      })
    })
  },
  methods: {
    // 跳转到歌曲详情页
    toSongDetail(songId) {
      this.$router.push('/discover/song/detail?id=' + songId)
    }
  }
}
</script>

<style scoped>
table {
  border: 1px solid #666;
  font-size: 14px;
  color: #fff;
  border-collapse: collapse;
}
th,
td {
  padding-left: 15px;
}
tr td:nth-child(1) {
  padding: 0;
  text-align: center;
}

/* 表格标题 */
th {
  text-align: left;
  height: 40px;
  border-bottom: 1px solid #666;
}
thead tr th:nth-child(1) {
  border-right: 1px solid #666;
  width: 70px;
}
thead tr th:nth-child(2) {
  border-right: 1px solid #666;
  width: 280px;
}
thead tr th:nth-child(3) {
  border-right: 1px solid #666;
  width: 130px;
}
thead tr th:nth-child(4) {
  width: 180px;
}

/* 表格前三行 */
img {
  vertical-align: middle;
  margin-right: 10px;
  width: 40px;
  height: 40px;
  cursor: pointer;
}
.behind {
  height: 50px;
}

/* 表格后97行 */
.after {
  height: 30px;
  line-height: 30px;
}
.play {
  display: inline-block;
  vertical-align: middle;
  margin-right: 8px;
  width: 20px;
  height: 20px;
  background: url(~@/assets/img/play.png);
  background-size: 20px 20px;
  cursor: pointer;
}
/* 歌曲名列过长不换行 */
.name {
  width: 260px;
}
.artist {
  width: 150px;
}
.name,
.artist {
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
  cursor: pointer;
}
/* 隔行变色 */
.current {
  background-color: #343434;
}
</style>